package com.beep.erp.common.utils;


import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.res.Configuration;
import android.os.CountDownTimer;
import android.util.Log;


import com.beep.erp.Constants;
import com.beep.erp.http.HttpRetrofit;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import static com.beep.erp.AppContext._context;

/**
 * Created by Administration on 2017/8/11.
 */

public class ToolsUtils {


    public static boolean isRunningForeground(Activity activity) {
        ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
        // 枚举进程
        for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfos) {
            if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                if (appProcessInfo.processName.equals(activity.getApplicationInfo().processName)) {
                    Log.d("", "EntryActivity isRunningForeGround");
                    return true;
                }
            }
        }
        Log.d("", "EntryActivity isRunningBackGround");
        return false;
    }

    public static boolean isEmpty(String str) {
//        Logger.d("str:" + str);
        str = StringUtils.replaceBlank(str);//去除空格
        if (str == null || str.length() == 0 || ("null".equals(str)) || ("(null)".equals(str))) {
            return true;
        } else {
            return false;
        }

    }

    public static String orderFields(Class clazz, Object object, long mills) {
        StringBuilder rawString = new StringBuilder();
        Map<String, String> map = new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            //用于访问私有变量\属性
            field.setAccessible(true);
            String valString;
            try {
                if (field.get(object) == null) {
                    valString = "";
                } else {
                    valString = field.get(object).toString();
                }
                map.put(field.getName(), valString);
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        Collection<String> keySet = map.keySet();
        List<String> list = new ArrayList<String>(keySet);
        Collections.sort(list);

        for (int i = 0; i < list.size(); i++) {
            System.out.print("参数=" + list.get(i));
            if (i == (list.size() - 1)) {
                if (!StringUtils.isEmpty(map.get(list.get(i)))) {
                    rawString.append(list.get(i)).append("=").append(map.get(list.get(i)));
                }
            } else {
                if (!StringUtils.isEmpty(map.get(list.get(i)))) {
                    rawString.append(list.get(i)).append("=").append(map.get(list.get(i))).append("&");
                }
            }
        }
        rawString.append(getParam(mills));
        return rawString.toString();
    }

    public static String buildSignStr(Map<String, String> params, long mills) {
        StringBuilder sb = new StringBuilder();
        // 将参数以参数名的字典升序排序
        Map<String, String> sortParams = new TreeMap<String, String>(params);
        // 遍历排序的字典,并拼接"key=value"格式
        for (Map.Entry<String, String> entry : sortParams.entrySet()) {
            if (sb.length() != 0) {
                sb.append("&");
            }
            sb.append(entry.getKey()).append("=").append(entry.getValue());
        }
        sb.append(getParam(mills));
        Log.d("param------------", sb.toString());
        return sb.toString();
    }

    public static String getParam(long mills) {
        SPUtils spUtils = new SPUtils(_context, Constants.EXTRA_LOGIN_SP);
        return "&timeStamp=" + mills
                + "&username=" + spUtils.getString(Constants.USER_NAME)
                + "&password=" + spUtils.getString(Constants.USER_PWD_MI);
    }

    public static String getParam1(long mills) {
        SPUtils spUtils = new SPUtils(_context, Constants.EXTRA_LOGIN_SP);
        return "timeStamp=" + mills
                + "&username=" + spUtils.getString(Constants.USER_NAME)
                + "&password=" + spUtils.getString(Constants.USER_PWD_MI);
    }

    public static boolean isLegalId(String id) {
        if (id.toUpperCase().matches("(^\\d{15}$)|(^\\d{17}([0-9]|X)$)")) {
            return true;
        } else {
            return false;
        }
    }

    public static <T> boolean isEmptyList(List<T> list) {
        if (null == list) {
            return true;
        } else if (("").equals(list)) {
            return true;
        } else if (("null").equals((list + ("")).trim())) {
            return true;
        } else if ((list + "").trim().equals("[]")) {
            return true;
        } else {
            return false;
        }

    }

    public static int stringParserInteger(String num) {
        if (ToolsUtils.isEmpty(num)) {
            return 0;
        }
        try {
            return Integer.parseInt(num);
        } catch (Exception e) {
            return 0;
        }

    }

    public static double stringParserDouble(String num) {
        if (ToolsUtils.isEmpty(num)) {
            return 0;
        }
        try {
            return Double.parseDouble(num);
        } catch (Exception e) {
            return 0;
        }

    }

    public static float stringParserFloat(String num) {
        if (ToolsUtils.isEmpty(num)) {
            return 0;
        }
        try {
            return Float.parseFloat(num);
        } catch (Exception e) {
            return 0;
        }

    }

    public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }
}
